Skip to content

Index color, piece and castling-side tables by their enums - #67

Merged
nook0110 merged 6 commits into
deploy-elofrom
enum-arrays
Nov 12, 2026
Merged

nook0110 merged 6 commits into
deploy-elofrom
enum-arrays

Conversation

@nook0110

@nook0110 nook0110 commented Nov 9, 2026

Copy link
Copy Markdown
Owner

Tables keyed by Player, Piece, CastlingSide and GamePhase are now indexed by the enum itself, so indexing a color table with a piece, or a castling side with a raw 0/1, no longer compiles. This finishes the EnumArray idea from the enum-indexed-array branch.

Change (4e51dca)

  • Chess/EnumArray.h: EnumArray<Key, Value> with operator[](Key), iteration and ==, with sizes registered next to each enum (kEnumSize<Player> = 2, …).
  • Aliases ByPlayer, ByPiece, ByCastlingSide, ByGamePhase, and the loop lists kPlayers, kCastlingSides, kPiecesByValue.
  • Converted:
    • board state;
    • castling, pawn and attack tables;
    • Zobrist keys, PSQT and piece values;
    • the evaluation tunables;
    • the FEN parser and the time control.
  • 154 of the 201 static_cast<size_t> casts are gone, along with the color_idx/piece_idx helper variables.
  • The TaperedEval phase pair and NNUE are untouched.

Verification (GCC 15, gate build, CPU 19, three alternating runs each)

  • Identical nodes and best moves on 30 positions.
  • Perft: startpos d5 = 4,865,609; kiwipete d4 = 4,085,603.
  • Instructions per node: 11,736 vs 11,728 (+0.07%).
  • Cycles per node: 13,478–13,631 vs 13,499–13,715.
  • Machine code differs only in index arithmetic: an imul instead of a lea/sub chain for the PSQT row, and one fewer saved register.
  • Tests: 206/206 pass in Release.

Stacking. Based on #65, because the FEN parser it converts comes from there. It also carries #66's two commits (b15e5d3, 8fb3af9) until #66 merges. Merge #63 → #65 and #66 → this.

🤖 Generated with Claude Code

nook0110 and others added 6 commits November 9, 2026 05:44
Move generation wrote through PseudoLegalMove*& cursors into
caller-owned std::array buffers, and legal moves came back in a
heap-allocated std::vector.

Add InplaceVector<T, N>, a fixed-capacity vector with the subset of the
std::inplace_vector interface the engine needs. std::inplace_vector
itself needs libstdc++ 16, and GCC 16 currently builds this engine
slower. GeneratePseudoLegalMoves and GenerateLegalMoves return
PseudoLegalMoves and LegalMoves by value, the helpers append with
unchecked_push_back, the en passant filter is erase_if, and callers use
range-for, std::ranges::contains and views::concat instead of pointer
loops. GenerateMoves duplicated GenerateLegalMoves and is gone. The
principal variation gets its own type bounded by the deepest depth, so
move generation and PV extraction no longer allocate.

Search is unchanged: identical nodes and best moves on 30 positions,
perft startpos d5 4865609 and kiwipete d4 4085603. The push loops keep
the size in a register and store it once, and the returned lists are
built in place without copies. Instructions per node +0.04%
(11708-11719 vs 11714-11721), cycles within noise.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
PartialInsertionSort took raw begin/end pointers and a limit that was
always INT64_MIN, so the partial part never applied. It becomes
SortByScore over a ScoredRange, a std::ranges::subrange of the scored
move array, written with iterators and std::prev. The killer lookup in
bad captures uses std::ranges::find with a &ScoredMove::move projection
instead of find_if over data() pointers.

The range is a subrange of std::array iterators rather than a std::span:
GCC 15 keeps an extra pointer copy per shifted element in loops over
span iterators, while array iterators compile to the same 7-instruction
inner loop as before. Search is unchanged (identical nodes and best
moves on 30 positions); instructions per node 11712-11716 vs
11710-11713, cycles per node 13468-13524 vs 13590-13602.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Tables keyed by Player, Piece, CastlingSide and GamePhase were plain
std::arrays indexed through static_cast<size_t>(enum) and helper
variables such as color_idx and piece_idx, so nothing stopped indexing a
color table with a piece or a castling side with a raw 0/1.

Add EnumArray<Key, Value>, an array whose operator[] takes the enum
itself, with ByPlayer, ByPiece, ByCastlingSide and ByGamePhase aliases
and kPlayers, kCastlingSides and kPiecesByValue lists for loops. Board
state, the castling, pawn and attack tables, Zobrist keys, PSQT, piece
values, the evaluation tunables, the FEN parser and the time control use
them; indexing with the wrong enum or an integer no longer compiles. The
TaperedEval phase pair and NNUE are unchanged. 154 of the 201 size_t
casts are gone.

Search is unchanged: identical nodes and best moves on 30 positions,
perft startpos d5 4865609 and kiwipete d4 4085603. Machine code differs
only in index arithmetic (an imul instead of a lea/sub chain for the
PSQT row, one fewer saved register); instructions per node 11736 vs
11728 (+0.07%), cycles per node 13478-13631 vs 13499-13715.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Keep the constexpr pawn, piece-square and Zobrist tables indexed by their enums. Reset the typed continuation-history array in bulk and preserve the inplace move lists and 32-byte magic descriptors.

Bench: 993713
@nook0110
nook0110 changed the base branch from uci-nodes to deploy-elo November 12, 2026 20:34
@nook0110
nook0110 merged commit 49ad4f7 into deploy-elo Nov 12, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant